package com.shurdroides.proyectofinal.activities;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.shurdroides.proyectofinal.R;
import com.shurdroides.proyectofinal.constants.Constants;
import com.shurdroides.proyectofinal.databasehelpers.DataHelper;
import com.shurdroides.proyectofinal.databasehelpers.OpenHelper;
import com.shurdroides.proyectofinal.map.MapHelper;
import com.shurdroides.proyectofinal.server.Helper;
import com.shurdroides.proyectofinal.server.HttpUtils;

public class InitActivity extends Activity implements OnClickListener,
		Constants {

	private Button btnGoToNuevoUsuario;
	private Button btnGoToMap;
	private Button btnGoToMezclas;
	private Button btnGoToManual;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initViews();
		checkFirstTime();
		// new
		// RequestAsyncTask().execute("http://api.geonames.org/postalCodeSearchJSON?postalcode=9011&maxRows=10&username=demo");
		// String
		// s=HttpUtils.getPage(this,"http://api.geonames.org/postalCodeSearchJSON?postalcode=9011&maxRows=10&username=demo"
		// );
		// Log.e("--TEST--","------------"+ s);
	}

	private void checkFirstTime() {
		

		if (Helper.getMyPreferences(this).getFirst()) {
			DataHelper data = Helper.getDataHelper(this);
			data.insertObject(0, "piedra_peq", "piedra", 3, 1, 4, 1,
					0, 1);
			data.insertObject(1, "piedra_med", "piedra", 3, 1, 1, 1,
					0, 1);
			data.insertObject(2, "martillo", "martillo", 3, 1, 1, 5,
					0, 5);
			data.insertObject(3, "cuerda", "cuerda", 3, 1, 1, 1, 0, 1);
			data.insertObject(4, "palo_corto", "palo", 3, 1, 1, 1, 0,
					1);
			data.insertObject(5, "palo_largo", "palo", 3, 1, 1, 1, 0,
					1);
			data.insertObject(6, "piedra_afilada", "sharp_stone", 3,
					0, 0, 1, 0, 0);
			data.insertObject(7, "hacha", "hacha", 3, 0, 0, 5, 0, 5);
			data.insertObject(8, "lanza", "spear", 3, 0, 0, 5, 0, 5);

			data.insertReceta(0, 3);
			data.insertReceta(1, 2);
			data.insertReceta(2, 3);
			data.insertReceta(3, 2);
			data.insertReceta(4, 1);
			data.insertReceta(5, 4);
			data.insertReceta(6, 4);
			data.insertReceta(7, 2);

			data.insertIngrediente(0, 1, 0, 1);
			data.insertIngrediente(1, 2, 0, 1);
			data.insertIngrediente(2, 0, 1, 1);
			data.insertIngrediente(3, 3, 1, 1);
			data.insertIngrediente(4, 4, 1, 1);
			
			 data.insertIngrediente(5, 0, 3, 2);
			 data.insertIngrediente(6, 2, 3, 1);
			 data.insertIngrediente(7, 0, 4, 2);
			 data.insertIngrediente(8, 1, 4, 1);
			 data.insertIngrediente(9, 6, 5, 1);
			 data.insertIngrediente(10, 4, 5, 1);
			 data.insertIngrediente(11, 3, 5, 1);
			 data.insertIngrediente(12, 6, 6, 1);
			 data.insertIngrediente(13, 5, 6, 1);
			 data.insertIngrediente(14, 3, 6, 1);
			 data.insertIngrediente(15, 7, 7, 1);
			 data.insertIngrediente(16, 5, 7, 1);
			 data.insertRes(6, 8, 7, 1);
			 data.insertRes(5, 8, 6, 1);
			 data.insertRes(4, 7, 5, 1);
			 data.insertRes(3, 6, 4, 1);
			 data.insertRes(2, 6, 3, 1);
			
			data.insertRes(1, 2, 1, 1);
			data.insertRes(0, 0, 0, 2);
			Helper.getMyPreferences(this).setFirst(false);
		} else {
			// btnGoToNuevoUsuario.setVisibility(View.INVISIBLE);
		}
	}

	private void initViews() {
		btnGoToNuevoUsuario = (Button) findViewById(R.id.btn_registrar_nuevo_usuario);
		btnGoToNuevoUsuario.setOnClickListener(this);
		btnGoToMap = (Button) findViewById(R.id.btn_tomap);
		btnGoToMap.setOnClickListener(this);
		btnGoToManual = (Button) findViewById(R.id.btn_init_next);
		btnGoToManual.setOnClickListener(this);
		btnGoToMezclas = (Button) findViewById(R.id.btn_tomezclas);
		btnGoToMezclas.bringToFront();
		btnGoToMezclas.setOnClickListener(this);
		ImageView v = (ImageView) findViewById(R.id.pteros_img);
		v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.pteros_anim));

	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);

		AnimationDrawable anim = (AnimationDrawable) findViewById(R.id.fire)
				.getBackground();
		AnimationDrawable anim2 = (AnimationDrawable) findViewById(
				R.id.reflejo_fuego).getBackground();
		anim2.start();
		anim.start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btn_registrar_nuevo_usuario:
			startActivity(new Intent(this, RegistrationUI.class));
			break;
		case R.id.btn_init_next:
			startActivity(new Intent(this, Manual.class));
			break;
		case R.id.btn_tomap:
			if (HttpUtils.isInternetEnabled(this)) {
				startActivity(new Intent(this, MapaActivity.class));
			} else {
				Toast.makeText(this, "Please, enable your internet connection",
						Toast.LENGTH_LONG).show();
			}

			break;
		case R.id.btn_tomezclas:
			if (Helper.getMyPreferences(this).getFirst()) {
				OpenHelper open = new OpenHelper(this);
			}

			try {
				if (MapHelper.dentroDelRadio(MapHelper
						.getMyActualPosition(this),
						Helper.getMyPreferences(this).getCueva(),
						Preferences.RADIO_CUEVA)) {
					startActivity(new Intent(this,
							GeneracionElementosCasaActivity.class));
				} else {
					startActivity(new Intent(this,
							GeneracionElementosCalleActivity.class));
				}
			} catch (Exception e) {
				startActivity(new Intent(this,
						GeneracionElementosCalleActivity.class));
			}
			break;
		}
	}
}